package doc2db.interfaces.action;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

/**
 *
 * @author yuriy
 */
public class DelTableAction extends CreateDialog implements ActionListener{

    public DelTableAction(JEditorPane __p, JEditorPane t2) {
        super(__p, t2);
    }

    public void actionPerformed(ActionEvent e) {
        if (!Keey.isConnected) {
            return;
        }
        MyJEditorPane mP = (MyJEditorPane) super.p;
        if (mP.isInTable) {
            super.res = mP.tableName;
        }
        if (res.equals("__NULL")) {
            this.show();
        }
        if (res.equals("__NULL")) {
            return;
        }

        int ret = JOptionPane.showConfirmDialog(null,
				"Удалить таблицу \""+res+"\"?","Подтверждение",
				JOptionPane.YES_NO_OPTION);
        if(ret == JOptionPane.OK_OPTION){
            UserProp up = new UserProp();
            String str = "DROP TABLE \"" + res + "\" ;";
            try {
                DbSelects query = new DbSelects(up.getURL(), up.getUserProp());
                if (query.sendQueryUpdate(str)) {
                    JOptionPane.showMessageDialog(null, "Таблица удалина",
                            "message", JOptionPane.WARNING_MESSAGE);
                }
                String s = "";
                s += this.e.getText();
                this.e.setText(s + "\n" + str);
                new TableListAction(super.p, super.e).actionPerformed(null);
            } catch (Exception ex) {
                if (Keey.debug) {
                    System.err.println(ex);
                }
            }
            res = "__NULL";
        }

    }


}
